package GUI;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.sql.SQLException;
import javax.swing.JLabel;
import Other.Glob;
import database.DatabaseExtension;
import enums.DialogType;
import enums.SearchType;
public class Dialog extends JDialog {
	String title;
	private static Dialog dialogInstance = null;
	private final JPanel contentPanel = new JPanel();
	private Dialog() {
		System.out.println("This is the global dialog constant inside the remove method " + Glob.dialog);
		System.out.println("This is the global type constant inside the remove method " + Glob.type);
		JLabel lblAreYouSure;
		setBounds(100, 100, 492, 117);
		getContentPane().setLayout(new BorderLayout());
		setResizable(false);
		setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			if(Glob.dialog == DialogType.DELETE){
				title = "Are you sure you want to delete this item?";
				}
			else if(Glob.dialog == DialogType.OVERWRITE){
				title = "Are you sure you want to overwrite this item?";
				}
				lblAreYouSure = new JLabel(title);
			contentPanel.add(lblAreYouSure);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Enter Ok");
				okButton.addActionListener(new ActionListener() {
					//set up listener for the button
					public void actionPerformed(ActionEvent e) {
							//calling the method
							ok();
						//set up a new searchtype  condition right here
					}
				});
				okButton.addKeyListener(new KeyAdapter() {
					@Override
					public void keyTyped(KeyEvent e) {
						//when a number is entered pressing Enter
						if(e.getKeyChar() == KeyEvent.VK_ENTER){
							System.out.println("This is the global variable when pressing enter in dialog " + Glob.type);
								ok();
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Esc Cancel");
				cancelButton.getInputMap(cancelButton.WHEN_IN_FOCUSED_WINDOW)
				.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "Search");
				cancelButton.getActionMap().put("Search", new AbstractAction() {
					 public void actionPerformed(ActionEvent e) {
						escape();
					    }
				});
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						escape();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	public void ok(){
		try {
			switch(Glob.type) {
			case COUNTRY:
				Country c = Country.getInstance();			
			if(Glob.dialog == DialogType.DELETE){
				DatabaseExtension.deleteItem(c.getFieldText());
				Dialog.getInstance().dispose();
				c.resetFields();
				c.enableCountry();
			}
			else if(Glob.dialog == DialogType.OVERWRITE){
				Dialog.getInstance().dispose();
				c.save();
				c.resetFields();
				c.enableCountry();
			}
			break;
			case PAYMENT:
				Payment p = Payment.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(p.getFieldText());
					Dialog.getInstance().dispose();
					p.resetFields();
					p.enablePayment();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					p.save();
					p.resetFields();
					p.enablePayment();
				}
				break;
			case INCOTERM:
				Incoterm i = Incoterm.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(i.getFieldText());
					Dialog.getInstance().dispose();
					i.resetFields();
					i.enableIncoterm();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					i.save();
					i.resetFields();
					i.enableIncoterm();
				}
				break;
			case TAX:
				Tax t = Tax.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(t.getFieldText());
					Dialog.getInstance().dispose();
					t.resetFields();
					t.enableTax();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					t.save();
					t.getInstance().resetFields();
					t.enableTax();
				}
				break;
				
			case ADDRESSEE:
				Addressee a = Addressee.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(a.getFieldCode());
					Dialog.getInstance().dispose();
					a.resetFields();
					a.enableAddressee();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					a.save();
					a.resetFields();
					a.enableAddressee();
				}
				break;
				
			case SUPPLIER:
				Supplier su = Supplier.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(su.getFieldCode());
					Dialog.getInstance().dispose();
					su.resetFields();
					su.enableSupplier();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					su.save();
					su.resetFields();
					su.enableSupplier();
				}
				break;
				
			case CLIENT:
				Client client = Client.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(client.getFieldCode());
					Dialog.getInstance().dispose();
					client.resetFields();
					client.enableClient();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					client.save();
					client.resetFields();
					client.enableClient();
				}
				break;
				
			case DESTINATION:
				Destination destination = Destination.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(destination.getFieldCode());
					Dialog.getInstance().dispose();
					destination.resetFields();
					destination.enableDestination();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					destination.save();
					destination.resetFields();
					destination.enableDestination();
				}
				break;
				
			case SHIPMENT:
				Shipment shipment = Shipment.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(shipment.getFieldCode());
					Dialog.getInstance().dispose();
					shipment.resetFields();
					shipment.enableShipment();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					shipment.save();
					shipment.resetFields();
					shipment.enableShipment();
				}
				break;
				
			case TRANSPORT:
				Transport transport = Transport.getInstance();
				if(Glob.dialog == DialogType.DELETE){
					DatabaseExtension.deleteItem(transport.getFieldCode());
					Dialog.getInstance().dispose();
					transport.resetFields();
					transport.enableTransport();
				}
				else if(Glob.dialog == DialogType.OVERWRITE){
					Dialog.getInstance().dispose();
					transport.save();
					transport.resetFields();
					transport.enableTransport();
				}
				break;
				// Enter new condition for type right here
			}
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
	public void escape(){
		switch(Glob.type) {
		case COUNTRY:
			Country c = Country.getInstance();
			//title = "Are you sure that you want to overwrite this item in Country?";
			Dialog.getInstance().dispose();
			c.enableCountry();
			break;
		case PAYMENT:
			Payment p = Payment.getInstance();
			Dialog.getInstance().dispose();
			p.enablePayment();
			break;
		case INCOTERM:
			Incoterm i = Incoterm.getInstance();
			Dialog.getInstance().dispose();
			i.enableIncoterm();
			break;
		case TAX:
			Tax t = Tax.getInstance();
			Dialog.getInstance().dispose();
			t.enableTax();
			break;
			
		case ADDRESSEE:
			Addressee a = Addressee.getInstance();
			Dialog.getInstance().dispose();
			a.enableAddressee();
			break;
			
		case SUPPLIER:
			Supplier su  = Supplier.getInstance();
			Dialog.getInstance().dispose();
			su.enableSupplier();
			break;
			
		case CLIENT:
			Client client  = Client.getInstance();
			Dialog.getInstance().dispose();
			client.enableClient();
			break;
			
		case DESTINATION:
			Destination destination  = Destination.getInstance();
			Dialog.getInstance().dispose();
			destination.enableDestination();
			break;
			
		case SHIPMENT:
			Shipment shipment  = Shipment.getInstance();
			Dialog.getInstance().dispose();
			shipment.enableShipment();
			break;
			
		case TRANSPORT:
			Transport transport  = Transport.getInstance();
			Dialog.getInstance().dispose();
			transport.enableTransport();
			break;
			// Enter new condition for type right here
	}
}
public static Dialog getInstance(){
		if(dialogInstance == null)
			dialogInstance = new Dialog();
		return dialogInstance;
		}
}
